package Nonogram;

/**
 * ErrorDetection
 * @author Rachel Wedewer,
 * Date Modified: 4/30/2014
 * 
 * This class is meant as a way to catch mistakes when marking the puzzle grid.  At the moment it
 * basically focuses on that whether there are more marked fill spaces then permitted with a line's given clues.  And
 * also making sure the remaining number of cells that would be marked blank do not make up more cells than they should.
 *  
 */

public class ErrorDetection {

	Nonogram controller;
	GridArray puzzleGrid;
	CluesArray rowCluesArray;
	CluesArray colCluesArray;
	
	 public ErrorDetection(Nonogram controller, Data data){
			this.controller = controller;
			this.puzzleGrid = data.getGrid();
			this.rowCluesArray = data.getRows();
			this.colCluesArray = data.getCols();
	 }
	
	 public boolean checkForErrorsBeforeRunning(){
          	boolean errors;
        	errors = errorRowDetection();
        	
        	if (errors){
        		return errors;
        	}
        	errors= errorColDetection();
        	
        	if (errors){
        		return errors;
        	}
        	return errors;
    }
	
	public boolean ErrDetection(){
      	boolean errors;
    	errors = errorRowDetection();
    	
    	if (errors){
    		return errors;
    	}
    	errors= errorColDetection();
    	
    	if (errors){
    		return errors;
    	}
    	controller.updateExplanation("\nNo Errors Found.");
    	return errors;
    }
    
	//Checks rows for errors
    private boolean errorRowDetection(){
      	int maxFilled=0;
    	int maxBlank =0;
    	int filled = 0;
    	int blank = 0;
    	String error = "";
    	//Go through board rows
    	for(int row = 0; row < puzzleGrid.getGrid().length; row++){
    		//first get maxes
    		maxFilled = 0;
    		
    		for(int clues= 0; clues < rowCluesArray.getLineClues(row).length; clues++){
    			maxFilled = maxFilled + rowCluesArray.getLineClues(row)[clues];
    		}
    		maxBlank = puzzleGrid.getGrid().length - maxFilled;
    		
    		//Go through line
    		filled = 0;
    		blank = 0;
    		for(int col = 0; col < puzzleGrid.getGrid().length; col++){
    			
    			//Determine what is Marked
    			if (puzzleGrid.getCellValue(row, col) == 0){
    				blank = blank +1;
    			}
    			else if (puzzleGrid.getCellValue(row, col) == 1){
    				filled = filled +1;}
    			
    			//Report errors in Rows
    			if (filled > maxFilled){
    				error = "\nERROR Found: Row "+ (row+1) + " contains too many FILLED cells."
    						+ "\nFix errors before continuing!";
    				controller.updateExplanation(error);
    				return true;
    			}
    			else if (blank > maxBlank){
    				error = "\nERROR Found: Row "+ (row+1) + " contains too many BLANK cells"
    						+ "\nFix errors before continuing!";
    				controller.updateExplanation(error);
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
  //Checks cols for errors
    private boolean errorColDetection(){
    	int maxFilled=0;
    	int maxBlank =0;
    	int filled = 0;
    	int blank = 0;
    	String error = "";
    	//Go through board rows
    	for(int col = 0; col < puzzleGrid.getGrid().length; col++){
    		//first get maxes
    		maxFilled = 0;
    		
    		for(int clues= 0; clues < colCluesArray.getLineClues(col).length; clues++){
    			maxFilled = maxFilled + colCluesArray.getLineClues(col)[clues];
    		}
    		maxBlank = puzzleGrid.getGrid().length - maxFilled;
    		
    		//Go through line
    		filled = 0;
    		blank = 0;
    		for(int row = 0; row < puzzleGrid.getGrid().length; row++){
    			//Determine what is Marked
       			if (puzzleGrid.getCellValue(row, col) == 0){
    				blank = blank +1;
    			}
    			else if (puzzleGrid.getCellValue(row, col) == 1){
    				filled = filled +1;}
    			
    			//Report errors in Cols
    			if (filled > maxFilled){
    				error = "\nERROR Found: Column "+ (col+1) + " contains too many FILLED cells"
    						+ "\nFix errors before continuing!";
    				controller.updateExplanation(error);
    				return true;
    			}
    			else if (blank > maxBlank){
    				error = "\nERROR Found: Column "+ (col+1) + " contains too many BLANK cells"
    						+ "\nFix errors before continuing!";
    				controller.updateExplanation(error);
    				return true;
    			}
    		}
    	}
    	return false;
    }
   
}
